package com.qire.manhua.model.viewModel.impl;

import android.view.View;
import android.widget.BaseAdapter;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.EventReceiver;
import com.qire.common.widget.adapter.auto.SummerWithTypeBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.RecommendModuleService;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleBottomMenuBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleItemOneBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleItemThreeBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleItemTwoBinding;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleItemEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.ViewDataBinding;

/**
 * 首页 -> 推荐 -> 基本组合模块。
 * 用于构建组合首页复合布局一个 {@link RecommendModuleViewModel} 就是一个模块，多复合模块由 {@link HomeModuleType} 根据集体类型来构建。
 * @see HomeModuleType#buildModuleViewModels
 */
@AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module)
public class RecommendModuleViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleBinding>, EventReceiver {

    private RecommendModuleService moduleService = new RecommendModuleService(this);

    /**
     * 基本元素件列表，一个模块可以通过存放不同类型的基本元素件来灵活调整布局。平铺布局会更具具体存放元素类型，来展示内容
     */
    private final ArrayList<ItemViewModel> tileItemList = new ArrayList<>();

    /**
     * 平铺布局适配器，会更具基本元素列表，通过适配器转换成对应布局控件存放入平铺布局里
     */
    private final SummerWithTypeBindAdapter tileAdapter = new SummerWithTypeBindAdapter(tileItemList);

    /**
     * 模块实体
     */
    private final HomeModuleEntity homeModuleEntity;

    /**
     * 基本组合模块构建函数
     * @param homeModuleEntity 一个模块实体
     */
    public RecommendModuleViewModel(HomeModuleEntity homeModuleEntity) {
        this.homeModuleEntity = homeModuleEntity;
    }

    @Override
    public void bindVariable(LayoutHomeRecommendModuleBinding viewBind) {
        viewBind.setRecommendModuleViewModel(this);
    }

    @Override
    public void onReceiver(AutoVariable eventProcess) {
        tileAdapter.setEventProcess(eventProcess);
    }

    public ModuleBottomMenuViewModel buildBottomMenu() {
        return new ModuleBottomMenuViewModel(this);
    }

    public void nextBatch() {
        moduleService.nextBatch();
    }

    public void clearTileItemList() {
        tileItemList.clear();
    }
    public void addModelItem(ItemViewModel itemViewModel) {
        this.tileItemList.add(itemViewModel);
    }
    public void refreshNextBatch() {
        int size = tileItemList.size();
        ArrayList<HomeModuleItemEntity> moduleItemList = homeModuleEntity.moduleItemList();
        for (int index = 0; index < size; index++) {
            tileItemList.get(index).updateItem(moduleItemList.get(index));
        }
    }

    public HomeModuleEntity homeModuleEntity() {
        return homeModuleEntity;
    }

    public int moduleId() {
        return homeModuleEntity.id;
    }
    public String moduleTitle() {
        return homeModuleEntity.name;
    }
    public int moduleNextPageNo() {
        return homeModuleEntity.pageNo() + 1;
    }

    @Bindable
    public CharSequence getModuleTitle() {
        return homeModuleEntity.name;
    }

    public BaseAdapter getTileAdapter() {
        return tileAdapter;
    }


    /**
     * 模块的基本元素件，独占一行的大漫画推荐位
     */
    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_item_one)
    public class ModuleOneColumnItemViewModel extends BaseObservable implements ItemViewModel<LayoutHomeRecommendModuleItemOneBinding> {

        private HomeModuleItemEntity moduleItemEntity;

        public ModuleOneColumnItemViewModel(HomeModuleItemEntity moduleItemEntity) {
            this.moduleItemEntity = moduleItemEntity;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleItemOneBinding viewBind) {
            viewBind.setModuleOneColumnItemViewModel(this);
        }

        @Override
        public void updateItem(HomeModuleItemEntity moduleItemEntity) {
            if(ObjectUtil.nonNull(moduleItemEntity)) {
                this.moduleItemEntity = moduleItemEntity;
                notifyChange();
            }
        }

        @Bindable
        public HomeModuleItemEntity getModuleItemEntity() {
            return moduleItemEntity;
        }

        @Bindable
        public String getCoverPath() {
            return moduleItemEntity.coverImg;
        }

        @Bindable
        public int getFloatTipsVisible() {
            return StringUtil.isEmpty(moduleItemEntity.floatTips) ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public CharSequence getFloatTips() {
            return moduleItemEntity.floatTips;
        }

        @Bindable
        public CharSequence getTitle() {
            return moduleItemEntity.bookName;
        }

        @Bindable
        public CharSequence getShortTitle() {
            return moduleItemEntity.title;
        }

    }

    /**
     * 模块中基本元素件，两个占一行的横放漫画推荐位
     */
    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_item_two)
    public class ModuleTwoColumnItemViewModel extends BaseObservable implements ItemViewModel<LayoutHomeRecommendModuleItemTwoBinding> {

        private final boolean lineBreak;

        private HomeModuleItemEntity moduleItemEntity;

        public ModuleTwoColumnItemViewModel(HomeModuleItemEntity moduleItemEntity, boolean lineBreak) {
            this.lineBreak = lineBreak;
            this.moduleItemEntity = moduleItemEntity;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleItemTwoBinding viewBind) {
            viewBind.setModuleTwoColumnItemViewModel(this);
        }

        @Override
        public void updateItem(HomeModuleItemEntity moduleItemEntity) {
            if(ObjectUtil.nonNull(moduleItemEntity)) {
                this.moduleItemEntity = moduleItemEntity;
                notifyChange();
            }
        }

        @Bindable
        public HomeModuleItemEntity getModuleItemEntity() {
            return moduleItemEntity;
        }

        @Bindable
        public String getCoverPath() {
            return moduleItemEntity.coverImg;
        }

        @Bindable
        public int getFloatTipsVisible() {
            return StringUtil.isEmpty(moduleItemEntity.floatTips) ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public CharSequence getFloatTips() {
            return moduleItemEntity.floatTips;
        }

        @Bindable
        public CharSequence getTitle() {
            return moduleItemEntity.bookName;
        }

        @Bindable
        public CharSequence getShortTitle() {
            if(StringUtil.isEmpty(moduleItemEntity.title)) {
                if(StringUtil.isEmpty(moduleItemEntity.getTag())) {
                    return moduleItemEntity.shortTitle;
                }
                return moduleItemEntity.getTag();
            }
            return moduleItemEntity.title;
        }

        @Bindable
        public boolean getLineBreak() {
            return lineBreak;
        }

    }

    /**
     * 模块中的基本元素件，三个占一行的竖放漫画推荐位
     */
    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_item_three)
    public class ModuleThreeColumnItemViewModel extends BaseObservable implements ItemViewModel<LayoutHomeRecommendModuleItemThreeBinding> {

        private final boolean lineBreak;

        private HomeModuleItemEntity moduleItemEntity;

        public ModuleThreeColumnItemViewModel(HomeModuleItemEntity moduleItemEntity, boolean lineBreak) {
            this.lineBreak = lineBreak;
            this.moduleItemEntity = moduleItemEntity;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleItemThreeBinding viewBind) {
            viewBind.setModuleThreeColumnItemViewModel(this);
        }

        @Override
        public void updateItem(HomeModuleItemEntity moduleItemEntity) {
            if(ObjectUtil.nonNull(moduleItemEntity)) {
                this.moduleItemEntity = moduleItemEntity;
                notifyChange();
            }
        }

        @Bindable
        public HomeModuleItemEntity getModuleItemEntity() {
            return moduleItemEntity;
        }

        @Bindable
        public String getCoverPath() {
            return moduleItemEntity.coverImg;
        }

        @Bindable
        public int getFloatTipsVisible() {
            return StringUtil.isEmpty(moduleItemEntity.floatTips) ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public CharSequence getFloatTips() {
            return moduleItemEntity.floatTips;
        }

        @Bindable
        public CharSequence getTitle() {
            return moduleItemEntity.bookName;
        }

        @Bindable
        public CharSequence getShortTitle() {
            return moduleItemEntity.getTag();
        }

        @Bindable
        public boolean getLineBreak() {
            return lineBreak;
        }

    }

    /**
     * 单独一个模块的底部菜单，包含2个按钮
     */
    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_bottom_menu)
    public class ModuleBottomMenuViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleBottomMenuBinding> {

        private final RecommendModuleViewModel moduleViewModel;

        public ModuleBottomMenuViewModel(RecommendModuleViewModel moduleViewModel) {
            this.moduleViewModel = moduleViewModel;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleBottomMenuBinding viewBind) {
            viewBind.setModuleBottomMenuViewModel(this);
        }

        @Bindable
        public RecommendModuleViewModel getModuleViewModel() {
            return this.moduleViewModel;
        }

    }

    /**
     * 为了标准化限制使用，
     * @param <VD>
     */
    public interface ItemViewModel<VD extends ViewDataBinding> extends AutoVariable<VD> {

        void updateItem(HomeModuleItemEntity moduleItemEntity);

    }

}
